<?php
	include_once( "config.inc.php" );
	include_once( "misql.inc.php" );
	
	$errt = "";	
	$locat = ""; 
	$cnx = new MiSQL( $serv, $user, $pss, $base );
	$f = 0;
	
	$cnx->cnxOn();
		$v1 = $cnx->consulta( "nombre, pass, nivel,login", "usuarios", "login = \"".htmlentities( $_POST['u'] )."\"" );
		if ( count($v1) > 0 ){
			$pas = sha1( htmlentities($_POST['p']));
			if ( $pas == $v1[0]['pass'] ){
				session_start();
				$_SESSION['auth'] = "OK";
				$_SESSION['nombre'] = $v1[0]['nombre']; 
				$_SESSION['correo'] = $v1[0]['correo']; 
				$_SESSION['login'] = $v1[0]['login'];
				$_SESSION['nivel'] = $v1[0]['nivel']; 
				$f = 2;
			}
			else {
				$f = 1;
			}
		} 
	$cnx->cnxOff();	
		
	switch ( $f ){
		case 0:
			$errt = "El usuario no existe en este sistema.<br>Por favor comuniquese a soporte técnico RIUADY.";
			break;
		case 1:
			$errt = "Las contraseñas no coinciden.<br>Por favor intente de nuevo.";
			break;
		case 2:
			 $locat = "indexA.php";
			break;
	}	
		
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Autentificación al Sistema</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">

		function redirect(){
			this.location.href = "<?php echo $locat; ?>";
		}

		function error(){
			document.uno.err.value = "<?php echo $errt; ?>";
			document.uno.submit(); 
		}

	</script>
</head>
<body onLoad="<?php if ( $f == 0 || $f == 1 ){ echo "error();"; }else{ echo "redirect();"; } ?>">
<form name="uno" action="index.php" method="post">
	<input type="hidden" name="err" value="" />
</form>
</body>
</html>
